package top.infopub.mgr.mkt.service.impl;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.apache.commons.lang3.StringEscapeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import top.infopub.generic.GenericDao;
import top.infopub.generic.GenericServiceImpl;
import top.infopub.mgr.common.vo.RspVo;
import top.infopub.mgr.mkt.dao.ProjectRiskDao;
import top.infopub.mgr.mkt.domain.RiskDomain;
import top.infopub.mgr.mkt.service.ProjectRiskService;
import top.infopub.mgr.mkt.vo.CollectionInfoVo;
import top.infopub.mgr.mkt.vo.RiskVo;
import top.infopub.model.mkt.NormalNum;
import top.infopub.model.mkt.ProjectRisk;
import top.infopub.security.util.SessionUserUtils;
import top.infopub.security.vo.AuthorizationVO;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class ProjectRiskServiceImpl extends GenericServiceImpl<ProjectRisk, String> implements ProjectRiskService {

    @Autowired
    private ProjectRiskDao projectRiskDao;

    @Override
    public GenericDao<ProjectRisk, String> getDao() {
        return projectRiskDao;
    }

    @Override
    public Page<RiskDomain> selectPage(Page<RiskDomain> page, RiskVo riskVo) {
        PageHelper.startPage(page.getPageNum(), page.getPageSize());
        return (Page<RiskDomain>) projectRiskDao.selectPageByDomain(riskVo);
    }

    @Override
    public List<ProjectRisk> selectByProCode(String proCode) {
        return projectRiskDao.selectByProCode(proCode);
    }

    @Override
    public Page<ProjectRisk> selectByTotalNum(Page<ProjectRisk> page,NormalNum normalNum) {
        PageHelper.startPage(page.getPageNum(), page.getPageSize());
        return (Page<ProjectRisk>)projectRiskDao.selectByTotalNum(normalNum);
    }

    @Override
    public Page<ProjectRisk> selectByNormalNum(Page<ProjectRisk> page,NormalNum normalNum) {
        PageHelper.startPage(page.getPageNum(), page.getPageSize());
        return (Page<ProjectRisk>)projectRiskDao.selectByNormalNum(normalNum);
    }

    @Override
    public Page<ProjectRisk> selectByHighLevel(Page<ProjectRisk> page,NormalNum normalNum) {
        PageHelper.startPage(page.getPageNum(), page.getPageSize());
        return (Page<ProjectRisk>)projectRiskDao.selectByHighLevel(normalNum);
    }

    @Override
    public Page<ProjectRisk> selectByMidLevel(Page<ProjectRisk> page,NormalNum normalNum) {
        PageHelper.startPage(page.getPageNum(), page.getPageSize());
        return (Page<ProjectRisk>)projectRiskDao.selectByMidLevel(normalNum);
    }

    @Override
    public Page<ProjectRisk> selectByLowLevel(Page<ProjectRisk> page,NormalNum normalNum) {
        PageHelper.startPage(page.getPageNum(), page.getPageSize());
        return (Page<ProjectRisk>)projectRiskDao.selectByLowLevel(normalNum);
    }

    @Override
    public RspVo updateByid(String bodyStr) {
        AuthorizationVO userDtl = SessionUserUtils.getSessionAttributeForUserDtl();
        String s = StringEscapeUtils.unescapeJava(bodyStr);
        CollectionInfoVo vo = JSON.parseObject(s,CollectionInfoVo.class);
        List<ProjectRisk> projectRisk1 = vo.getProjectRisk();
        List<ProjectRisk> risks = new ArrayList<>();
        if (projectRisk1!=null && projectRisk1.size()>0){
            for (ProjectRisk projectRisk : projectRisk1) {
                 if ("1".equals(projectRisk.getIsStatus())){
                     projectRisk.setUpdateBy(userDtl.getId());
                     projectRisk.setUpdateTime(new Date());
                     projectRisk.setStatus("1");
                     risks.add(projectRisk);
                 }
            }
        }
        if (risks.size()>0){
            projectRiskDao.updateByBatch(risks);
        }
        return RspVo.getSuccessResponse();
    }
}
